package com.it.hanzhongtravelv1.controller;

import com.it.hanzhongtravelv1.dto.AccommodationDTO;
import com.it.hanzhongtravelv1.entity.Accommodation;
import com.it.hanzhongtravelv1.service.AccommodationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/accommodations")
@CrossOrigin(origins = "*")
public class AccommodationController {

    @Autowired
    private AccommodationService accommodationService;

    @GetMapping
    public ResponseEntity<List<AccommodationDTO>> getAllAccommodations() {
        return ResponseEntity.ok(accommodationService.getAllAccommodations());
    }

    @GetMapping("/{id}")
    public ResponseEntity<AccommodationDTO> getAccommodationById(@PathVariable Long id) {
        AccommodationDTO accommodation = accommodationService.getAccommodationById(id);
        if (accommodation != null) {
            return ResponseEntity.ok(accommodation);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping
    public ResponseEntity<AccommodationDTO> createAccommodation(@RequestBody Accommodation accommodation) {
        return ResponseEntity.ok(accommodationService.saveAccommodation(accommodation));
    }

    @PutMapping("/{id}")
    public ResponseEntity<AccommodationDTO> updateAccommodation(@PathVariable Long id, @RequestBody Accommodation accommodation) {
        accommodation.setId(id);
        return ResponseEntity.ok(accommodationService.saveAccommodation(accommodation));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAccommodation(@PathVariable Long id) {
        accommodationService.deleteAccommodation(id);
        return ResponseEntity.ok().build();
    }
}